//
//  HomeListCell.swift
//  SwitfTest
//
//  Created by runone02 on 2018/12/28.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

import UIKit

class HomeListCell: UITableViewCell {
    // 定义闭包
    typealias callBlock = (_ index:Int,_ str:String)->()
    typealias heightBlock = (_ height:CGFloat)->()
    // 设置闭包属性
    var sendValueBlock:callBlock!
    ///高度回调
     var heightBlock:heightBlock!
    // 无参数闭包的定义
    typealias otherBlock = ()->()
    var noneParmBlock:otherBlock!

    /// 标题
    var titleLabel:UILabel!
    /// 正文
    var contentLabel: UILabel!
    /// 图片
    var iconImageView:UIImageView!
    /// 底部标题    1
    var bottomLabel:UILabel!
    /// 时间
    var timeLabel:UILabel!
    /// 删除按钮
    var deleteButton:UIButton!
    /// 加载更多
    var lodeButton:UIButton!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.selectionStyle = UITableViewCell.SelectionStyle(rawValue: 0)!
        self.separatorInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: -10)

        setUpUI()
    }
    
    func setUpUI() {
        
        titleLabel = UILabel()
        titleLabel.font = UIFont.systemFont(ofSize: 15)
        titleLabel.text = "标题"
        titleLabel.textColor = UIColor.black
        self .addSubview(titleLabel)
        titleLabel .mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(10)
            make?.top.mas_equalTo()(self)?.offset()(5)
            make?.height.mas_equalTo()(10)
        }

        lodeButton = UIButton(type: .custom)
        lodeButton.setTitle("更多", for: .normal)
        lodeButton.setTitle("收起", for: .selected)
        lodeButton.setTitleColor(UIColor.white, for: .normal)
        lodeButton.backgroundColor = UIColor.black
        lodeButton.addTarget(self, action: #selector(lodeButtonClick(button:)), for:.touchUpInside)
        self.addSubview(lodeButton)
        lodeButton.mas_makeConstraints { (make) in
            make?.bottom.mas_equalTo()(self)?.offset()(-5)
            make?.right.mas_equalTo()(self)?.offset()(-5)
            make?.height.mas_equalTo()(20)
            make?.width.mas_equalTo()(30)         
        }
        
        deleteButton = UIButton(type: .custom)
        deleteButton.setTitle("删除", for: .normal)
        deleteButton.setTitle("已删除", for: .selected)
        deleteButton.titleLabel?.font = UIFont.systemFont(ofSize: 10)
        deleteButton.backgroundColor = UIColor.darkGray
        deleteButton.setTitleColor(UIColor.black, for: .normal)
        deleteButton.layer.masksToBounds = true
        deleteButton.layer.borderColor = UIColor.lightGray.cgColor
        deleteButton.layer.borderWidth = 1
        deleteButton.layer.cornerRadius = 20
        deleteButton.addTarget(self, action: #selector(deleteButtonClick), for: .touchUpInside)
        self.addSubview(deleteButton)
        deleteButton.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(-5)
            make?.bottom.mas_equalTo()(lodeButton.mas_top)?.offset()(-10)
            make?.width.mas_equalTo()(40)
            make?.height.mas_equalTo()(40)
        }
        
        iconImageView = UIImageView()
        iconImageView.backgroundColor = UIColor.black
        iconImageView.layer.masksToBounds = true
        iconImageView.layer.cornerRadius = 10
        iconImageView.layer.borderWidth = 1
        iconImageView.layer.borderColor = UIColor.red.cgColor
        self.addSubview(iconImageView)
        iconImageView.mas_makeConstraints { (maek) in
            maek?.right.mas_equalTo()(self)?.offset()(-5)
            maek?.width.mas_equalTo()(30)
            maek?.top.mas_equalTo()(self)?.offset()(5)
            maek?.bottom.mas_equalTo()(deleteButton.mas_top)?.offset()(-5)
        }
   
        contentLabel = UILabel()
        contentLabel.font = UIFont.systemFont(ofSize: 14)
        contentLabel.text = "设置的内容"
        contentLabel.textColor = UIColor.black
        contentLabel.backgroundColor = UIColor.red
        contentLabel.numberOfLines = 4
        self.addSubview(contentLabel)
        contentLabel .mas_makeConstraints { (maek) in
            maek?.left.mas_equalTo()(titleLabel.mas_left)
            maek?.right.mas_equalTo()(iconImageView.mas_left)?.offset()(-10)
            maek?.top.mas_equalTo()(titleLabel.mas_bottom)?.offset()(15)
            maek?.height.mas_equalTo()(20)
        }
        
        bottomLabel = UILabel()
        bottomLabel.textColor = UIColor.black
        bottomLabel.font = UIFont.systemFont(ofSize: 15)
        bottomLabel.text = "底部文字"
        self.addSubview(bottomLabel)
        bottomLabel.mas_makeConstraints { (maek) in
            maek?.top.mas_equalTo()(contentLabel.mas_bottom)?.offset()(5)
            maek?.bottom.mas_equalTo()(self)?.offset()(-5)
            maek?.left.mas_equalTo()(10)
            maek?.width.mas_equalTo()(80)
        }
        
        timeLabel = UILabel()
        timeLabel.text = "时间"
        timeLabel.font = UIFont.systemFont(ofSize: 15)
        timeLabel.textColor = UIColor.black
        self.addSubview(timeLabel)
        timeLabel .mas_makeConstraints { (make) in
            make?.left.mas_equalTo()(bottomLabel.mas_right)?.offset()(5)
            make?.centerY.mas_equalTo()(bottomLabel)
            make?.width.mas_equalTo()(80)
        }
    }

    ///加载更多
   @objc func lodeButtonClick(button:UIButton){
      button.isSelected = !button.isSelected
//    if button.isSelected {
//        if self.heightBlock != nil {
//            self.heightBlock(50)
//        }
//        contentLabel.mas_updateConstraints { (make) in
//            make?.height.mas_equalTo()(50)
//        }
//    }else{
//        if self.heightBlock != nil {
//            self.heightBlock(20)
//        }
//        contentLabel.mas_updateConstraints { (make) in
//            make?.height.mas_equalTo()(20)
//        }
//    }
    
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    @objc func deleteButtonClick(sender:UIButton){
        if self.sendValueBlock != nil {
            self.sendValueBlock(1,(sender.titleLabel?.text)!)
        }
        
        if self.noneParmBlock != nil {
            //无参闭包
            self.noneParmBlock()
        }

    }

}
